<template>
    <div class="preview-container">
        <!-- 头部导航 -->
        <div class="header">
            <span class="title">战略微头条</span>
            <el-button type="text" @click="handleExitPreview">退出预览</el-button>
        </div>

        <!-- 手机样式容器 -->
        <div class="phone-frame">
            <!-- 手机顶部状态栏 -->
            <div class="status-bar">
                <img src="../pic/insight-web-ui/insight-web-ui/assets/phone-head.png" alt="手机状态栏" class="status-img" />
            </div>

            <!-- 手机内容区域（添加滚动容器） -->
            <div class="phone-content" ref="scrollContainer">
                <h2 class="page-title">战略微头条</h2>
                
                <!-- 只显示一个新闻周报容器 -->
                <div class="news-item">
                    <!-- 标题使用列表中第一条数据的标题 -->
                    <h3>{{ list[0]?.title || '最新战略动态' }}</h3>
                    <div class="meta">
                        <span>👁️ {{ list[0]?.readCount || 0 }}人阅读</span>
                        <span>🕒 {{ list[0]?.newsTime || formatDate(new Date()) }}</span>
                    </div>
                    <div class="section">
                        <h4>◆ ◆ 新闻周报 ◆ ◆</h4>
                        <div class="news-list">
                            <!-- 遍历所有数据，按顺序显示数字 -->
                            <div class="news-card" v-for="(item, index) in list" :key="item.id">
                                <span class="number">{{ (index + 1).toString().padStart(2, '0') }}</span>
                                <p>{{ item.abstractReal || '暂无内容' }}</p>
                                <el-button type="text" class="read-more">阅读全文</el-button>
                                <!-- 纯CSS实现的分割线 -->
                                <div class="slash-divider"></div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 模拟空白区域，增强滚动效果 -->
                <div style="height: 50px;"></div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const route = useRoute()
const router = useRouter()
const list = ref([])
const scrollContainer = ref(null)

// 格式化日期为 YYYY-MM-DD
const formatDate = (date) => {
    return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`
}

onMounted(() => {
    const listStr = route.query.list
    if (listStr) {
        list.value = JSON.parse(listStr)
    }
    // 监听滚动事件
    if (scrollContainer.value) {
        scrollContainer.value.addEventListener('scroll', handleScroll)
    }
})

// 退出预览
const handleExitPreview = () => {
    router.back()
    ElMessage.success('已退出预览')
}

// 滚动时隐藏/显示滚动条
const handleScroll = (e) => {
    const container = e.target
    container.classList.toggle('hide-scrollbar', container.scrollTop > 0)
}

// 组件销毁时移除事件监听
onUnmounted(() => {
    if (scrollContainer.value) {
        scrollContainer.value.removeEventListener('scroll', handleScroll)
    }
})
</script>

<style scoped>
/* 保持原有样式不变 */
.preview-container {
    padding: 24px;
    background-color: #fff;
    min-height: 100vh;
}

.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;
}

.title {
    font-size: 18px;
    font-weight: 600;
}

.phone-frame {
    width: 375px;
    height: 667px;
    margin: 0 auto;
    border: 10px solid #000;
    border-radius: 40px;
    overflow: hidden;
    position: relative;
}

.status-bar {
    width: 100%;
    background-color: #fff;
    box-sizing: content-box;
}
.status-img {
    width: 100%;
    height: auto;
    display: block;
}

.phone-content {
    padding: 15px;
    height: calc(100% - 44px);
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: #c0c4cc #f2f3f5;
    transition: all 0.3s ease;
}
.phone-content::-webkit-scrollbar {
    width: 6px;
}
.phone-content::-webkit-scrollbar-track {
    background: #f2f3f5;
    border-radius: 3px;
}
.phone-content::-webkit-scrollbar-thumb {
    background: #c0c4cc;
    border-radius: 3px;
}
.phone-content.hide-scrollbar {
    scrollbar-width: none;
}
.phone-content.hide-scrollbar::-webkit-scrollbar {
    display: none;
}

.page-title {
    text-align: center;
    margin-bottom: 20px;
    font-size: 18px;
    font-weight: 600;
}

.news-item {
    margin-bottom: 20px;
}

.meta {
    display: flex;
    gap: 15px;
    color: #606266;
    font-size: 13px;
    margin-bottom: 15px;
}

.section h4 {
    text-align: center;
    margin-bottom: 15px;
    font-size: 16px;
    color: #409eff;
}

.news-list {
    display: flex;
    flex-direction: column;
    gap: 15px;
}

.news-card {
    border-bottom: 1px solid #eee;
    padding-bottom: 15px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.number {
    font-size: 24px;
    color: #909399;
    display: inline-block;
    margin-bottom: 5px;
}

.read-more {
    color: #409eff;
    font-size: 13px;
    align-self: flex-end;
}

/* 纯CSS实现的三条斜线分割线 */
.slash-divider {
    height: 6px;
    display: flex;
    align-items: center;
    margin-top: 8px;
}
.slash-divider::before {
    content: '';
    flex: 1;
    height: 1px;
    background-color: #409EFF;
}
.slash-divider::after {
    content: '///';
    color: #409EFF;
    margin-left: 5px;
    font-weight: bold;
}
</style>